var database = require('../../data/data.js');
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
var app = getApp();
Page({
  data: {
    openid: '',
    tabbar: {},
    pageNum: 1,
    noData: false,
  },
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'NRIBZ-OFHHR-DZMWB-W5EIN-FQXME-E5FC2'
    });
    database.count = 0;
    database.all = 2;
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    wx.request({
      url: database.url + '/project/uploadSuccessContestEnd.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
    })
    wx.request({
      url: database.url + '/project/getCarousel.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success(res) {
        database.all = database.all + res.data.length
        that.setData({
          swiper: res.data
        })
      }
    })
    wx.request({
      url: database.url + '/project/getNotice.action',
      header: {
        'content-type': 'applica tion/x-www-form-urlencoded;charset=utf-8',
      },
      success(res) {
        that.setData({
          announcement: res.data
        })
      }
    })
    wx.login({
      success(res) {
        database.code = res.code;
        wx.request({
          url: database.url1 + '/api/V2/user/access_token',
          data: {
            clientId: "QqKTO5BTPN6REI26QAgq",
            clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
          },
          success(res) {
            database.accessToken = res.data.data.accessToken;
            wx.request({
              url: database.url + '/api/V2/integral/integral_openid',
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                code: database.code,
                accessToken: database.accessToken,
                weChatId: "woaizuqiuyuezhanguangchang",
                regionName: "other"
              },
              success(res) {
                var sessoionKey = res.data.data.session_key;
                wx.getUserInfo({
                  data: {
                    withCredentials: true,
                    lang: "zh_CN"
                  },
                  success(res) {
                    wx.request({
                      url: database.url + '/api/V2/user/we_chat_user',
                      method: "POST",
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        accessToken: database.accessToken,
                        weChatId: "woaizuqiuyuezhanguangchang",
                        encryptedData: res.encryptedData,
                        iv: res.iv,
                        sessionKey: sessoionKey,
                        weChatName: "我爱足球约战广场"
                      },
                      success(res) {
                        database.unionid = res.data.data.unionId;
                        wx.request({
                          url: database.url1 + '/api/V2/user/verify_wechat_small',
                          method: "POST",
                          header: {
                            'content-type': 'application/x-www-form-urlencoded'
                          },
                          data: {
                            unionid: database.unionid,
                            accessToken: database.accessToken
                          },
                          success(res) {
                            console.log(res)
                            if (res.data.data.verify == "no") {
                              database.login = false;
                              database.count++;
                              if (database.count > database.all - 2) {
                                wx.hideLoading();
                              }
                            } else {
                              database.userId = res.data.data.id;
                              database.login = true;
                              wx.request({
                                url: database.url1 + '/api/V2/user/get_user',
                                data: {
                                  accessToken: database.accessToken,
                                  userId: database.userId
                                },
                                method: "POST",
                                header: {
                                  'content-type': 'application/x-www-form-urlencoded'
                                },
                                success(res) {
                                  database.tel = res.data.data.mobile;
                                  database.count++;
                                  if (database.count > database.all - 2) {
                                    wx.hideLoading();
                                  }
                                }
                              })
                            }
                          }
                        })
                        wx.getLocation({
                          success: function (res) {
                            wx.request({
                              url: database.url + '/project/getContestIndex.action',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
                              },
                              data: {
                                mylat: res.latitude,
                                mylng: res.longitude,
                                pageNum: that.data.pageNum
                              },
                              success(res) {
                                var date1 = new Date();
                                var contents = res.data;
                                var noData = false;
                                if (contents.length < 4) {
                                  noData = true;
                                } else {
                                  that.data.pageNum++;
                                  noData = false;
                                }
                                var teams = [];
                                var index = 0;
                                var contentss = [];
                                for (var i = 0; i < contents.length; i++) {
                                  var reg = new RegExp("-", "g");
                                  var newTime = contents[i].postdate.replace(reg, "/");
                                  var date = new Date(newTime);
                                  var seconds = 0;
                                  var minutes = 0;
                                  var hour = 0;
                                  var day = 0;
                                  var month = 0;
                                  var year = 0;
                                  var timeDifference = "";
                                  if ((date1 - date) / 1000 >= 60) {
                                    if ((date1 - date) / 1000 / 60 >= 60) {
                                      if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                                        if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                                          if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                                            year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                                            timeDifference = year + "年前";
                                          } else {
                                            month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                                            timeDifference = month + "个月前";
                                          }
                                        } else {
                                          day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                                          timeDifference = day + "天前";
                                        }
                                      } else {
                                        hour = parseInt((date1 - date) / 1000 / 60 / 60);
                                        timeDifference = hour + "小时前";
                                      }
                                    } else {
                                      minutes = parseInt((date1 - date) / 1000 / 60);
                                      timeDifference = minutes + "分钟前";
                                    }
                                  } else {
                                    seconds = parseInt((date1 - date) / 1000);
                                    timeDifference = seconds + "秒前";
                                  };
                                  var content = {
                                    address: contents[i].adress,
                                    cost: contents[i].cost,
                                    date: contents[i].date,
                                    distance: contents[i].distance,
                                    gameid: contents[i].gameid,
                                    person: contents[i].person,
                                    pteamid: contents[i].pteamid,
                                    popenid: contents[i].popenid,
                                    sort: contents[i].sort,
                                    time: contents[i].time,
                                    week: contents[i].week,
                                    teamLogo: contents[i].pteamlogo,
                                    title: contents[i].pteamname,
                                    postdate: contents[i].postdate,
                                    timeDifference: timeDifference
                                  }
                                  contentss[i] = content;
                                }
                                that.setData({
                                  contents: contentss,
                                  noData: noData
                                })
                              }
                            })
                          }
                        })
                      }
                    })
                  }
                })
              }
            })
          }
        })
        wx.getUserInfo({
          lang: "zh_CN ",
          success(e) {
            var data = e.userInfo;
            database.nickName = data.nickName;
            database.avatarUrl = data.avatarUrl;
          }
        });
      }
    })
    if (options.city != null) {
      this.setData({
        city: options.city,
      })
      database.city = options.city;
    }
    this.setData({
      city: database.city,
    })
    if (database.city == "未知") {
      qqmapsdk.reverseGeocoder({
        success: function (data) {
          var city = data.result.address_component.city;
          if (city.indexOf("市")) {
            city = city.substring(0, city.length - 1)
          }
          that.setData({
            city: city
          })
          database.city = city
        }
      })
    }
    this.setData({
      announcement: database.announcement
    })
    app.editTabBar();
  },
  onReachBottom: function () {
    var that = this;
    if (!this.data.noData) {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      wx.getLocation({
        success: function (res) {
          wx.request({
            url: database.url + '/project/getContestIndex.action',
            header: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            data: {
              mylat: res.latitude,
              mylng: res.longitude,
              pageNum: that.data.pageNum
            },
            success(res) {
              wx.hideLoading();
              var date1 = new Date();
              var contents = res.data;
              var noData = false;
              if (contents.length < 4) {
                noData = true;
              } else {
                noData = false;
                that.data.pageNum++;
              }
              var teams = [];
              var index = 0;
              var contentss = [];
              var contentsss = [];
              for (var i = 0; i < contents.length; i++) {
                var reg = new RegExp("-", "g");
                var newTime = contents[i].postdate.replace(reg, "/");
                var date = new Date(newTime);
                var seconds = 0;
                var minutes = 0;
                var hour = 0;
                var day = 0;
                var month = 0;
                var year = 0;
                var timeDifference = "";
                if ((date1 - date) / 1000 >= 60) {
                  if ((date1 - date) / 1000 / 60 >= 60) {
                    if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                      if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                        if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                          year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                          timeDifference = year + "年前";
                        } else {
                          month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                          timeDifference = month + "个月前";
                        }
                      } else {
                        day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                        timeDifference = day + "天前";
                      }
                    } else {
                      hour = parseInt((date1 - date) / 1000 / 60 / 60);
                      timeDifference = hour + "小时前";
                    }
                  } else {
                    minutes = parseInt((date1 - date) / 1000 / 60);
                    timeDifference = minutes + "分钟前";
                  }
                } else {
                  seconds = parseInt((date1 - date) / 1000);
                  timeDifference = seconds + "秒前";
                };
                var content = {
                  address: contents[i].adress,
                  cost: contents[i].cost,
                  date: contents[i].date,
                  distance: contents[i].distance,
                  gameid: contents[i].gameid,
                  person: contents[i].person,
                  pteamid: contents[i].pteamid,
                  popenid: contents[i].popenid,
                  sort: contents[i].sort,
                  time: contents[i].time,
                  week: contents[i].week,
                  teamLogo: contents[i].pteamlogo,
                  title: contents[i].pteamname,
                  postdate: contents[i].postdate,
                  timeDifference: timeDifference
                }
                contentss[i] = content;
              }
              for (var i = 0; i < that.data.contents.length + contentss.length; i++) {
                if (i < that.data.contents.length) {
                  contentsss[i] = that.data.contents[i];
                } else {
                  contentsss[i] = contentss[i - that.data.contents.length];
                }
              }
              that.setData({
                contents: contentsss,
                noData: noData
              })
            }
          })
        }
      })
    }
  },
  add: function () {
    wx.navigateTo({
      url: '/pages/fight/fight',
    })
  },
  switch_city: function (e) {
    wx.navigateTo({
      url: '../switchcity/switchcity?city=' + e.currentTarget.dataset.city,
    })
  },
  search: function (e) {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  detail: function (e) {
    wx.navigateTo({
      url: '/pages/fight_detail/battle/battle?gameid=' + e.currentTarget.dataset.gameid + '&popenid=' + e.currentTarget.dataset.popenid,
    })
  },
  swiperDetail: function (e) {
    wx.request({
      url: database.url + '/project/uploadAddCarouselViews.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        id: e.currentTarget.dataset.id
      },
      success(res) {
      }
    })
    wx.navigateTo({
      url: '/pages/home/swiper_detail/swiper_detail?id=' + e.currentTarget.dataset.id,
    })
  },
  announcementDetail: function (e) {
    wx.request({
      url: database.url + '/project/uploadAddNoticeViews.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        id: e.currentTarget.dataset.id
      },
      success(res) {
      }
    })
    wx.navigateTo({
      url: '/pages/home/announcement_detail/announcement_detail?id=' + e.currentTarget.dataset.id,
    })
  },
  imageOnLoad: function () {
    database.count++;
    if (database.count > database.all - 2) {
      wx.hideLoading();
    }
  },
  onShareAppMessage() {
    return {
      title: '发布比赛 约战球队 邀请队友报名',
      imageUrl: '/image/share.png',
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})